import 'package:flutter/material.dart';
import '../../../models/device.dart';

class DeviceCard extends StatelessWidget {
  final Device device;
  final Function(bool) onToggle;
  final VoidCallback onTap;

  const DeviceCard({
    Key? key,
    required this.device,
    required this.onToggle,
    required this.onTap,
  }) : super(key: key);

  IconData _getDeviceIcon() {
    switch (device.type) {
      case 'light':
        return Icons.lightbulb;
      case 'thermostat':
        return Icons.thermostat;
      case 'lock':
        return Icons.lock;
      case 'camera':
        return Icons.videocam;
      case 'speaker':
        return Icons.speaker;
      default:
        return Icons.devices_other;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 2.0,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12.0),
      ),
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(12.0),
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Icon(
                    _getDeviceIcon(),
                    size: 28.0,
                    color: device.status ? Colors.blue : Colors.grey,
                  ),
                  Switch(
                    value: device.status,
                    onChanged: onToggle,
                    activeColor: Colors.blue,
                  ),
                ],
              ),
              const SizedBox(height: 8.0),
              Text(
                device.name,
                style: const TextStyle(
                  fontSize: 16.0,
                  fontWeight: FontWeight.bold,
                ),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
              const SizedBox(height: 4.0),
              Text(
                device.status ? '已开启' : '已关闭',
                style: TextStyle(
                  fontSize: 14.0,
                  color: device.status ? Colors.green : Colors.grey,
                ),
              ),
              if (device.type == 'thermostat' && device.attributes.containsKey('temperature'))
                Padding(
                  padding: const EdgeInsets.only(top: 4.0),
                  child: Text(
                    '温度: ${device.attributes['temperature']}°C',
                    style: const TextStyle(fontSize: 14.0),
                  ),
                ),
            ],
          ),
        ),
      ),
    );
  }
}